import java.util.concurrent.CountDownLatch;

/**
 * 编写代码, 基于 CountDownLatch 模拟跑步比赛的过程
 * Date: 2022-09-28
 * Time: 16:42
 */

public class Demo4 {
    public static void main(String[] args) throws InterruptedException {
        // 10个选手参加跑步比赛
        CountDownLatch count = new CountDownLatch(10);

        // 创建 10 个线程来执行跑步任务.
        for (int i = 0; i < 10; i++) {
            int k = i;
            Thread.sleep(50);

            Thread t = new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+"开始出发!");
                try {
                    // 跑步时间
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"到达终点!");

                // 选手撞线.
                count.countDown();
            });
            t.start();
        }
        count.await();
        System.out.println("比赛结束!");
    }
}
